﻿--status panel 
--durability code come from Tukui

  
--get the addon namespace
local addon, ns = ...
  
--get the config values
local LabelUI = ns.LabelUI
local cfg = ns.cfg

local noshow = LabelUI.noshow
local Kill = LabelUI.Kill
local point = LabelUI.point
local InitConfigFrame = LabelUI.InitConfigFrame
local AddChatFunc = LabelUI.AddChatFunc
local textureBackground
local texturehilite
local barbg1 
local barbg2 
local textureBackground2
local classcolor
local playerclass
local SaveVars
local repshow = true

local factionstand = {
	[0] = "Unknown",
	[1] = "Hated",
	[2] = "Hostile",
	[3] = "Unfriendly",
	[4] = "Neutral",
	[5] = "Friendly",
	[6] = "Honored",
	[7] = "Revered",
	[8] = "Exalted",
}

local LoadConfig = function()
	textureBackground = cfg.textureBackground
	texturehilite = cfg.texturehilite
	textureBackground2 = cfg.textureBackground2
	classcolor = cfg.classcolor 
	playerclass = cfg.playerclass 
	SaveVars = LabelUI.SaveVars	
	barbg1  = {
		bgFile = "Interface\\AddOns\\LabelUI\\media\\bubbles",
	}
	barbg2  = {
		bgFile = "Interface\\AddOns\\LabelUI\\media\\bubbles2",
	}
end

local function commify(num)
	if strlen(tostring(num)) <= 3 or type(num) ~= "number" then
		return num
	end
	local str = ""
	local count = 0
	for d in string.gmatch(strrev(tostring(num)), "%d") do
		if count ~= 0 and mod(count, 3) == 0 then
			str = str .. "," .. d
		else
			str = str .. d
		end
		count = count + 1
	end
	return strrev(str)
end

local function SetMinMaxValues(self, minv, maxv)
	self.max = maxv	
	if maxv	> minv then
		self.min = minv
	end
end

local function SetValue(self, value, minvalue)
	local percent = 0
	if minvalue then
		percent = minvalue/self.max		
	end
	if value < self.min then
		value = self.min
	end
	local percentleft = min((value - self.min), (self.max - self.min))/(self.max - self.min)
	local width = self:GetParent():GetWidth()
	local right = width*(1 - percentleft) 
	local left = width*percent
	self:SetPoint("RIGHT", self:GetParent(), "RIGHT", -right, 0)
	self:SetPoint("LEFT", self:GetParent(), "LEFT", left, 0)
	self:SetTexCoord(percent,percentleft,0,1)
	if self.bg and self.bg2 and self:IsShown() then
		self.bg:SetPoint("RIGHT", self:GetParent(), "RIGHT", -right, 0)
		self.bg:SetTexCoord(0,percentleft,1,1)
		self.bg2:SetPoint("LEFT", self.bg, "RIGHT", 0, 0)		
	end
end

local expbar

local function callback()
	if expbar:IsShown() then
		LabelUI.TTPoints:Set(20, expbar, 0, 3)
	else
		LabelUI.TTPoints:Set(20, false)
	end
	if not LabelUI.CallBack.exprepbar then return end
	for _,func in pairs(LabelUI.CallBack.exprepbar) do
		func()
	end
end

local function UpdateXP()
	expbar.curXP = UnitXP("player")
	expbar.maxXP = UnitXPMax("player")
	expbar.restedXP  = GetXPExhaustion()
	expbar.percent = expbar.curXP/expbar.maxXP
	if not expbar.xp:IsShown() then
		return
	end
	expbar.xp:SetMinMaxValues(0 ,expbar.maxXP)
	expbar.xp:SetValue(expbar.curXP)
	if not expbar.restedXP then
		expbar.xpr:Hide()
	else
		expbar.xpr:Show()
		expbar.xpr:SetMinMaxValues(0,expbar.maxXP)
		expbar.xpr:SetValue(expbar.restedXP + expbar.curXP, expbar.curXP)
	end
end

local function UpdateRep(standing, min, max, value)
	if not standing then
		standing, min, max, value = select(2, GetWatchedFactionInfo())
	end
	expbar.repb.curREP = value - min
	expbar.repb.maxREP = max - min
	expbar.repb.curSTND  = standing
	expbar.repb.percent = expbar.repb.curREP/expbar.repb.maxREP
	expbar.repb:SetMinMaxValues(0 ,expbar.repb.maxREP)
	expbar.repb:SetValue(expbar.repb.curREP)
	local color = _G.FACTION_BAR_COLORS[standing];
    expbar.repb:SetVertexColor(color.r, color.g, color.b, 0.65)
end

local function EnterExp()
	if expbar.maxlevel and not repshow then
		return
	end
	--GameTooltip:SetOwner(expbar, "ANCHOR_TOPLEFT", 0, 0) ANCHOR_PRESERVE
	--GameTooltip:SetOwner(expbar, "ANCHOR_NONE", 0, 0)
	GameTooltip_SetDefaultAnchor(GameTooltip, expbar)
	GameTooltip:ClearLines()
	if not expbar.maxlevel then
		GameTooltip:AddDoubleLine("Exp:",commify(expbar.curXP).."/"..commify(expbar.maxXP).." ("..(floor(expbar.percent*1000)/10).."%)", 1, 1, 1)
		GameTooltip:AddDoubleLine("To level:",commify(expbar.maxXP - expbar.curXP).." ("..(floor((1 - expbar.percent)*1000)/10).."%)", 1, 1, 1)
		if expbar.restedXP then
			GameTooltip:AddDoubleLine("Rested:",commify(expbar.restedXP).." ("..(floor(((expbar.curXP + expbar.restedXP)/expbar.maxXP)*1000)/10).."%)", 1, 1, 1)
		end
		GameTooltip:Show()
	end
	if repshow then
		if not expbar.maxlevel then
			GameTooltip:AddLine(" ")
		end
		GameTooltip:AddDoubleLine("Reputation:",expbar.repb.faction, 1, 1, 1)
		local color = _G.FACTION_BAR_COLORS[expbar.repb.curSTND];
		GameTooltip:AddDoubleLine(" ",factionstand[expbar.repb.curSTND], 1,1,1, color.r, color.g, color.b)
		GameTooltip:AddDoubleLine(" ",commify(expbar.repb.curREP).."/"..commify(expbar.repb.maxREP).." ("..(floor(expbar.repb.percent*1000)/10).."%)", 1, 1, 1)
		if not expbar.maxlevel and not expbar.repb.always then
			expbar.xp:Hide()
			expbar.xpr:Hide()
			expbar.repb:Show()
			UpdateRep()
		end
	end
	GameTooltip:Show()
end

local function UpdateBarVisible(faction)
	if UnitLevel("player") == MAX_PLAYER_LEVEL then 
		expbar:UnregisterEvent("PLAYER_XP_UPDATE")
		expbar:UnregisterEvent("PLAYER_UPDATE_RESTING")
		expbar:UnregisterEvent("PLAYER_LEVEL_UP")
		expbar.xp:Hide()
		expbar.xpr:Hide()
		expbar.maxlevel = true
	end
	faction = faction or GetWatchedFactionInfo()
	expbar.repb.faction = faction
	if faction and (SaveVars:Get("exprepbar|rep|always", false) or expbar.maxlevel) then
		expbar.xp:Hide()
		expbar.xpr:Hide()
		repshow = true
		expbar.repb.always = true
		expbar.repb:Show()
		UpdateRep()
	elseif faction and SaveVars:Get("exprepbar|rep|onmouse", false) then
		repshow = true
		expbar.repb.always = false
		UpdateRep()
	elseif repshow then
		repshow = false
		expbar.repb.always = false
		expbar.repb:Hide()
		if expbar.maxlevel then
			expbar.xp:Show()
			expbar.xp:SetMinMaxValues(0 ,1)
			expbar.xp:SetValue(0)
			expbar.xp:Hide()
		else
			expbar.xp:Show()
			UpdateXP()
		end
	end
end

local function Event(self, event, ...)
 	if event == "PLAYER_XP_UPDATE" or event == "PLAYER_UPDATE_RESTING" then
		UpdateXP()
	elseif event == "PLAYER_LEVEL_UP" then
		UpdateBarVisible()
	elseif event == "UPDATE_FACTION" then
		local name, standing, min, max, value = GetWatchedFactionInfo()
		if name ~= expbar.repb.faction then
			UpdateBarVisible(name)
		end
		if name and repshow then
			UpdateRep(standing, min, max, value)
		end
	end
end

local function CreateBar()
	if not expbar then
		expbar = CreateFrame("Frame", "LUI_ExpRepBar", UIParent)
		expbar:SetPoint("BOTTOMRIGHT", "LUI_Panel", "TOPRIGHT", 0, 3)
		expbar:SetPoint("BOTTOMLEFT", "LUI_Panel", "TOPLEFT", 0, 3)
		expbar:SetHeight(10)
		expbar:SetFrameStrata("BACKGROUND")
		expbar:SetScript("OnEnter", EnterExp)
		expbar:SetScript("OnLeave", function() 
			GameTooltip:Hide() 
			if not expbar.repb.always and repshow and not expbar.maxlevel then
				expbar.repb:Hide()
				expbar.xp:Show()
				UpdateXP()
			end
		end)
		expbar.xp = expbar:CreateTexture(nil, "BORDER")
		expbar.xp:SetPoint("BOTTOM", expbar, "BOTTOM", 0, 0)
		expbar.xp:SetPoint("TOP", expbar, "TOP", 0, 0)
		expbar.xp:SetTexture("Interface\\AddOns\\LabelUI\\media\\bubbles2")
		expbar.xp.SetMinMaxValues = SetMinMaxValues
		expbar.xp.SetValue = SetValue
		expbar.xp:SetMinMaxValues(0,1)
		expbar.xp:SetValue(0)
		expbar.xp:SetVertexColor(classcolor.r, classcolor.g, classcolor.b, 0.65)
		expbar.xp.bg = expbar:CreateTexture(nil, "BORDER")
		expbar.xp.bg:SetPoint("BOTTOM", expbar, "BOTTOM", 0, 0)
		expbar.xp.bg:SetPoint("TOP", expbar, "TOP", 0, 0)
		expbar.xp.bg:SetPoint("LEFT", expbar, "LEFT", 0, 0)
		expbar.xp.bg:SetTexture("Interface\\AddOns\\LabelUI\\media\\bubbles")
		expbar.xp.bg:SetVertexColor(0,0,0,0.5)
		expbar.xp.bg2 = expbar:CreateTexture(nil, "BORDER")
		expbar.xp.bg2:SetPoint("BOTTOM", expbar, "BOTTOM", 0, 0)
		expbar.xp.bg2:SetPoint("TOP", expbar, "TOP", 0, 0)
		expbar.xp.bg2:SetPoint("RIGHT", expbar, "RIGHT", 0, 0)
		expbar.xp.bg2:SetTexture("Interface\\AddOns\\LabelUI\\media\\SolidBorder")
		expbar.xp.bg2:SetVertexColor(0,0,0,0.5)
		expbar.xpr = expbar:CreateTexture(nil, "BORDER")
		expbar.xpr:SetPoint("BOTTOM", expbar, "BOTTOM", 0, 0)
		expbar.xpr:SetPoint("TOP", expbar, "TOP", 0, 0)
		expbar.xpr:SetTexture("Interface\\AddOns\\LabelUI\\media\\bubbles2")
		expbar.xpr.SetMinMaxValues = SetMinMaxValues
		expbar.xpr.SetValue = SetValue
		expbar.xpr:SetMinMaxValues(0,1)
		expbar.xpr:SetValue(0)
		expbar.xpr.bg = expbar.xp.bg
		expbar.xpr.bg2 = expbar.xp.bg2
		expbar.xpr:SetVertexColor(1,0,0,0.65)
		expbar.repb = expbar:CreateTexture(nil, "BORDER")
		expbar.repb:SetPoint("BOTTOM", expbar, "BOTTOM", 0, 0)
		expbar.repb:SetPoint("TOP", expbar, "TOP", 0, 0)
		expbar.repb:SetTexture("Interface\\AddOns\\LabelUI\\media\\bubbles2")
		expbar.repb.SetMinMaxValues = SetMinMaxValues
		expbar.repb.SetValue = SetValue
		expbar.repb:SetMinMaxValues(0,1)
		expbar.repb:SetValue(0)
		expbar.repb.bg = expbar.xp.bg
		expbar.repb.bg2 = expbar.xp.bg2
		local color = _G.FACTION_BAR_COLORS[5];
    	expbar.repb:SetVertexColor(color.r, color.g, color.b,0.65)
		expbar.repb:Hide()
		
		expbar:RegisterEvent("PLAYER_XP_UPDATE")
		expbar:RegisterEvent("PLAYER_UPDATE_RESTING")
		expbar:RegisterEvent("PLAYER_LEVEL_UP")
		expbar:RegisterEvent("UPDATE_FACTION")
		
		
		expbar:SetScript("OnEvent", Event)
		UpdateBarVisible()
		UpdateXP()
	else
		expbar:Show()
		UpdateBarVisible()
		UpdateXP()	
	end
	callback()
end

local function CreateConfigChanger()
	local curent
	local all = {
		[1] = "enable",
		[2] = "disable",	
	}
	if SaveVars:Get("exprepbar|show", false) then
		curent = all[1]
	else
		curent = all[2]
	end
	local elementtable = {
		[1] = {
			type = "label",
			point = { a1 = "TOPRIGHT", a2 = "BOTTOM", af = "self.title", x = 0, y = 0 }, 
			point2 = { a1 = "TOPLEFT", a2 = "BOTTOMLEFT", af = "self.title", x = 0, y = 0 }, 	
			height = 30,
			font = "Interface\\AddOns\\LabelUI\\media\\Aquarion.otf",
			size = 14,
			justifyh = "LEFT",
			text = "Exp\Rep Bar:",
			name = "exprepbar",
		},
		[2] = {
			type = "dropdown",
			point = { a1 = "LEFT", a2 = "RIGHT", af = "self.named['exprepbar']", x = 0, y = 0 }, 
			point2 = { a1 = "RIGHT", a2 = "RIGHT", af = "self.title", x = 0, y = 0 }, 	
			all = all,
			curent = curent,
			font = "Interface\\AddOns\\LabelUI\\media\\Aquarion.otf",
			size = 13,
			func = function(self)
	  	 		if self.text:GetText() == self.all[1] then
					self:GetParent().named['replabel']:Enable()
					self:GetParent().named['showalways']:Enable()
					if not self:GetParent().named['showalways']:GetChecked() then
						self:GetParent().named['showonmouseover']:Enable()
					end
				else
					self:GetParent().named['replabel']:Disable()	
					self:GetParent().named['showalways']:Disable()	
					self:GetParent().named['showonmouseover']:Disable()	
      			end	
			end,
			name = "expbardropdown",
		},
		[3] = {
			type = "label",
			--point = { a1 = "TOPRIGHT", a2 = "BOTTOMRIGHT", af = "self.named['exprepbar']", x = 0, y = 0 }, 
			point = { a1 = "TOPRIGHT", a2 = "BOTTOM", af = "self.title", x = 0, y = 0 }, 
			point2 = { a1 = "TOPLEFT", a2 = "BOTTOMLEFT", af = "self.named['exprepbar']", x = 0, y = 0 }, 	
			height = 15,
			font = "Interface\\AddOns\\LabelUI\\media\\Aquarion.otf",
			size = 14,
			justifyh = "LEFT",
			text = "Reputation:",
			name = "replabel",
		},
		[4] = {
			type = "checkbox",
			point = { a1 = "TOPLEFT", a2 = "BOTTOMRIGHT", af = "self.named['replabel']", x = 0, y = -2 },
			font = "Interface\\AddOns\\LabelUI\\media\\Aquarion.otf",
			size = 14,
			justifyh = "LEFT",
			text = "show always",
			checked = SaveVars:Get("exprepbar|rep|always", false),
			disable = curent == all[2],
			func = function(self)
	  	 		if self:GetChecked() then
					self:GetParent().named['showonmouseover']:Disable()
				else
					self:GetParent().named['showonmouseover']:Enable()	
      			end	
			end,
			name = "showalways",
		},
		[5] = {
			type = "checkbox",
			point = { a1 = "TOPLEFT", a2 = "BOTTOMLEFT", af = "self.named['showalways']", x = 0, y = 0 }, 
			font = "Interface\\AddOns\\LabelUI\\media\\Aquarion.otf",
			size = 14,
			justifyh = "LEFT",
			text = "show on mouse over",
			checked = SaveVars:Get("exprepbar|rep|onmouse", false),
			disable = SaveVars:Get("exprepbar|rep|always", false) or curent == all[2],
			name = "showonmouseover",
		},
	}
	local func = function(self)
	  	if self.named['expbardropdown'].text:GetText() ~= curent then
	  	 	if self.named['expbardropdown'].text:GetText() == all[1] then
				SaveVars:Set("exprepbar|show", true)	
				CreateBar()
			else
				SaveVars:Set("exprepbar|show", false)
				if expbar then
					expbar:Hide()
					callback()
				end 	
			end
      	end
		SaveVars:Set("exprepbar|rep|always", self.named['showalways']:GetChecked())	
		SaveVars:Set("exprepbar|rep|onmouse", self.named['showonmouseover']:GetChecked())
		UpdateBarVisible(f)
	end
	InitConfigFrame(elementtable, 250, 350, func)
end

local function Init()

	LoadConfig()

	if SaveVars:Get("exprepbar|show", false) then
		CreateBar()
	end

	AddChatFunc("ExpBar", CreateConfigChanger)

end

LabelUI.CallBack.exprepbar = {}
LabelUI:AddInit(Init, "ExpRepBar")

